<?php
namespace App\Services;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Redis;
use App\Exceptions\WechatException;

class WechatService {
    private static $appId;
    private static $appSecret;

    public function __construct()
    {
        self::$appId = config('services.wechat.appId');

        self::$appSecret = config('services.wechat.appSecret');
    }

    /**
     * @notes 发起公共请求
     * @param $path
     * @param $data
     * @param $methods
     * @return array|mixed
     * @throws \Illuminate\Http\Client\RequestException
     * @author Evil丿
     * @date 2024/04/02 14:10
     */
    private function commonRequest($path, $data, $methods = "GET")
    {
        $url = "https://api.weixin.qq.com" . $path;

        if($methods == "POST") {
            $res = Http::timeout(3)
                -> post($url, $data)
                -> throw()
                -> json();
        } else {
            $res = Http::timeout(3)
                -> get($url, $data)
                -> throw()
                -> json();
        }

        if(key_exists('errcode', $res) && $res['errcode'] > 0) {
            throw new WechatException($res['errmsg'], $res['errcode']);
        }

        return $res;
    }

    /**
     * @notes 获取微信请求令牌
     * @return mixed
     * @author Evil丿
     * @date 2024/04/02 14:10
     */
    private function getAccessToken()
    {
        $access_token = Redis::get('wechat:access_token');

        if($access_token) {
            return $access_token;
        }

        $data = [
            "appId"     => config('services.wechat.appId'),
            "secret"    => config('services.wechat.appSecret'),
            "grant_type"=> 'client_credential'
        ];

        $path = "/cgi-bin/token";

        $res = $this->commonRequest($path, $data);

        Redis::setex('wechat:access_token', 5400, $res['access_token']);

        return $res['access_token'];
    }

    /**
     * @notes 获取用户OPENID
     * @param $code
     * @return array|mixed
     * @throws \Illuminate\Http\Client\RequestException
     * @author Evil丿
     * @date 2024/04/02 14:16
     */
    public function getOpenId($code)
    {
        $path = "/sns/jscode2session";

        $data = [
            "appId"     => self::$appId,
            "secret"    => self::$appSecret,
            "js_code"   => $code,
            "grant_type"=> 'authorization_code'
        ];

        return $this->commonRequest($path, $data);
    }

    /**
     * @notes 获取用户手机号码
     * @param $code
     * @return mixed
     * @throws \Illuminate\Http\Client\RequestException
     * @author Evil丿
     * @date 2024/04/02 15:43
     */
    public function getPhoneNumber($code)
    {
        $data = [
            "code"         => $code
        ];

        $access_token = $this->getAccessToken();

        $path = "/wxa/business/getuserphonenumber?access_token=".$access_token;

        $res = $this->commonRequest($path, $data, "POST");

        return $res['phone_info'];
    }
}
